package opeartion;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-23
 * Time: 19:27
 */
public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的名字: ");
        String name = scanner.nextLine();

        int size = bookList.getUsedSize();
        int pos = -1;
        for (int i = 0; i < size; i++) {
            Book book = bookList.getPos(i);
            if (book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        //开始删除
        if (pos != -1) {
            for (int i = pos; i < size-1; i++) {
                Book book = bookList.getPos(i+1);
                bookList.setBooks(i, book);
            }
            bookList.setUsedSize(size - 1);
            System.out.println("删除成功!");
            return;
        }
        System.out.println("删除失败!");
    }
}
